package com.yxw.yxnet_cd_center.customer_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yxnet_cd_center.common.constant.RedisIdConstant;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.customer_service.dto.UserWorkUpdateDTO;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsUserEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsUserWorkEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.UserMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.UserWorkMapper;
import com.yxw.yxnet_cd_center.customer_service.service.UserWorkService;
import com.yxw.yxnet_cd_center.customer_service.utils.RedisManager;
import com.yxw.yxnet_cd_center.customer_service.utils.UserUtil;
import com.yxw.yxnet_cd_center.customer_service.vo.UserWorkInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author fangmaoxin
 * @version 1.0
 * @date 2023年9月14日
 * 客服工作设置服务类
 */
@Service
public class UserWorkServiceImpl extends ServiceImpl<UserWorkMapper, YxCsUserWorkEntity> implements UserWorkService {

    @Autowired
    private UserUtil userUtil;
    @Autowired
    private RedisManager oRedisManager;

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录成功后获取当前登录客服的设置信息
     */
    @Override
    public UserWorkInfoVo getUserWorkInfoByUserId(Long userId) {
        if (userId != null) {
            YxCsUserWorkEntity userWorkEntity = baseMapper.selectOne(new LambdaQueryWrapper<YxCsUserWorkEntity>()
                    .eq(YxCsUserWorkEntity::getUserId, userId));
            UserWorkInfoVo userWorkInfoVo = new UserWorkInfoVo();
            userWorkInfoVo.setUserId(userId);
            YxCsUserEntity yxCsUserEntity = userMapper.selectById(userId);
            if (yxCsUserEntity != null) {
                userWorkInfoVo.setUserName(yxCsUserEntity.getUserName());
                List<String> stringList = userMapper.getUserGroupNames(userId);
                if (!stringList.isEmpty()) {
                    String groupNames = String.join(",", stringList);
                    if (userWorkEntity == null) {
                        // 初始化新数据到数据库
                        userWorkEntity = new YxCsUserWorkEntity();
                        setUserWorkEntityInfo(userId, userWorkEntity, yxCsUserEntity, groupNames);
                        baseMapper.insert(userWorkEntity);
                    } else {
                        // 更新GroupName信息
                        userWorkEntity.setGroupName(groupNames);
                        baseMapper.updateById(userWorkEntity);
                    }
                }
            }
            BeanUtils.copyProperties(userWorkEntity, userWorkInfoVo);
            return userWorkInfoVo;
        }
        return null;
    }

    private static void setUserWorkEntityInfo(Long userId, YxCsUserWorkEntity userWorkEntity, YxCsUserEntity yxCsUserEntity, String groupNames) {
        userWorkEntity.setUserId(userId);
        userWorkEntity.setUserName(yxCsUserEntity.getUserName());
        userWorkEntity.setNickName(yxCsUserEntity.getNickName());
        userWorkEntity.setGroupName(groupNames);
        userWorkEntity.setCloseType(0);
        userWorkEntity.setHisTopType(0);
        userWorkEntity.setHisTopTime(0);
        userWorkEntity.setMaxNum(0);
        userWorkEntity.setOneSoundType(0);
        userWorkEntity.setOneBoxType(0);
        userWorkEntity.setOneSmsType(0);
        userWorkEntity.setGroupSoundType(0);
        userWorkEntity.setGroupBoxType(0);
        userWorkEntity.setGroupSmsType(0);
        userWorkEntity.setMessCallType(0);
        userWorkEntity.setTimeoutNum(0);
    }

    /**
     * 新增客服设置信息
     */
    @Override
    @Transactional
    public void save(UserWorkUpdateDTO updateDTO, Long userId) {
        YxCsUserWorkEntity userWork = new YxCsUserWorkEntity();
        BeanUtils.copyProperties(updateDTO, userWork);
        getGroupName(userId, userWork);
        initNickName(updateDTO, userId, userWork);
        this.save(userWork);
    }

    private void initNickName(UserWorkUpdateDTO updateDTO, Long userId, YxCsUserWorkEntity userWork) {
        if(userId != null && updateDTO.getNickName() == null){
            YxCsUserEntity yxCsUserEntity = userMapper.selectById(userId);
            if (yxCsUserEntity != null && null != yxCsUserEntity.getNickName()) {
                userWork.setNickName(yxCsUserEntity.getNickName());
            }
        }
    }

    private void getGroupName(Long userId, YxCsUserWorkEntity userWork) {
        if (userId != null) {
            YxCsUserEntity yxCsUserEntity = userMapper.selectById(userId);
            if (yxCsUserEntity != null) {
                userWork.setUserId(userId);
                userWork.setUserName(yxCsUserEntity.getUserName());
                List<String> stringList = userMapper.getUserGroupNames(userId);
                if (!stringList.isEmpty()) {
                    String groupNames = String.join(",", stringList);
                    userWork.setGroupName(groupNames);
                }
            }
        }
    }

    /**
     * 修改客服设置信息
     */
    @Override
    @Transactional
    public void update(UserWorkUpdateDTO updateDTO, Long userId) {
        YxCsUserWorkEntity userWork = this.getById(updateDTO.getWorkId());
        if (userWork == null) {
            throw new BaseException("当前客服设置信息不存在！");
        }
        BeanUtils.copyProperties(updateDTO, userWork);
        userWork.setUpdateTime(new Date());
        if (userUtil.getLoginName() != null) {
            userWork.setUpdateName(userUtil.getLoginName());
        }
        if (userUtil.getLoginUserId() != null) {
            userWork.setUpdateId(userUtil.getLoginUserId());
        }
        getGroupName(userId, userWork);
        this.updateById(userWork);
    }


    /**
     * 根据用户Id获取用户全局参数
     */
    @Override
    public YxCsUserWorkEntity getUserWorkGlobPara(Long userId) {
        YxCsUserWorkEntity oYxCsUserWorkEntity = (YxCsUserWorkEntity) oRedisManager.get(RedisIdConstant.SYSTEM_REDIS_USERPARM_FILED_SERVICE_PREFIX.concat(userId.toString()));
        if (null == oYxCsUserWorkEntity) {
            QueryWrapper<YxCsUserWorkEntity> oYxCsUserWorkEntityQueryWrapper = new QueryWrapper<>();
            oYxCsUserWorkEntityQueryWrapper.eq("user_id", userId);
            List<YxCsUserWorkEntity> userWorkList = baseMapper.selectList(oYxCsUserWorkEntityQueryWrapper);
            if (!userWorkList.isEmpty()) {
                oYxCsUserWorkEntity = userWorkList.get(0);
            }
        }
        return oYxCsUserWorkEntity;
    }
}
